package com.example.foobar.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.foobar.business.service.DaOfficialAccountService;
import com.example.foobar.business.service.ImsAccountWechatsService;
import com.example.foobar.dao.service.DaOfficialAccountDaoService;
import com.example.foobar.dao.service.ImsAccountWechatsDaoService;
import com.example.foobar.entity.DaOfficialAccount;
import com.example.foobar.entity.ImsAccountWechats;
import com.example.foobar.mapper.DaOfficialAccountMapper;
import com.example.foobar.param.DaOfficialAccountPageParam;
import io.geekidea.springbootplus.framework.common.service.impl.BaseServiceImpl;
import io.geekidea.springbootplus.framework.core.pagination.PageInfo;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 本地公众号信息 服务实现类
 *
 * @author 郑英奔
 * @since 2021-05-31
 */
@Slf4j
@Service
public class DaOfficialAccountServiceImpl implements DaOfficialAccountService {

    @Autowired
    private DaOfficialAccountDaoService daOfficialAccountDaoService;

    @Autowired
    private ImsAccountWechatsService imsAccountWechatsService;

    public boolean saveDaOfficialAccount(DaOfficialAccount daOfficialAccount) throws Exception {
        return daOfficialAccountDaoService.save(daOfficialAccount);
    }

    public boolean updateDaOfficialAccount(DaOfficialAccount daOfficialAccount) throws Exception {
        return daOfficialAccountDaoService.updateById(daOfficialAccount);
    }

    public boolean deleteDaOfficialAccount(Long id) throws Exception {
        return daOfficialAccountDaoService.removeById(id);
    }

    public Paging<DaOfficialAccount> getDaOfficialAccountPageList(DaOfficialAccountPageParam daOfficialAccountPageParam) throws Exception {
        return daOfficialAccountDaoService.getDaOfficialAccountPageList(daOfficialAccountPageParam);
    }

    @Override
    public boolean syncOfficialAccount() {
        List<ImsAccountWechats> imsAccountWechats = imsAccountWechatsService.selectList(new LambdaQueryWrapper<>(ImsAccountWechats.class));
        List<DaOfficialAccount> collect = imsAccountWechats.stream().map(this::imsAccountWechatsToDaOfficialAccount).collect(Collectors.toList());
        collect.stream().forEach(daOfficialAccount -> {
            LambdaQueryWrapper<DaOfficialAccount> wrapper = new LambdaQueryWrapper<>(DaOfficialAccount.class).eq(DaOfficialAccount::getUniacid, daOfficialAccount.getUniacid());
            DaOfficialAccount one = daOfficialAccountDaoService.getOne(wrapper);
            if(one != null){
                one.setName(daOfficialAccount.getName());
                try {
                    daOfficialAccountDaoService.updateDaOfficialAccount(one);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }else{
                daOfficialAccountDaoService.save(daOfficialAccount);
            }
        });
        return true;
    }

    public DaOfficialAccount getById(Long id) {
        return daOfficialAccountDaoService.getById(id);
    }

    @Override
    public List<DaOfficialAccount> list() {
        return daOfficialAccountDaoService.list();
    }

    @Override
    public DaOfficialAccount selectOne(LambdaQueryWrapper<DaOfficialAccount> wrapper) {
        return daOfficialAccountDaoService.selectOne(wrapper);
    }

    @Override
    public List<DaOfficialAccount>  list(Long userId) {

        return daOfficialAccountDaoService.getDaOfficialAccountListByUserId(userId);
    }

    private DaOfficialAccount imsAccountWechatsToDaOfficialAccount(ImsAccountWechats imsAccountWechats){
        Date now = new Date();
        DaOfficialAccount daOfficialAccount = new DaOfficialAccount();
        daOfficialAccount.setName(imsAccountWechats.getName());
        daOfficialAccount.setUniacid(imsAccountWechats.getUniacid());
        daOfficialAccount.setUpdateTime(now);

        return daOfficialAccount;
    }

}
